package br.com.unifebe.tcc.UTILS;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

import br.com.unifebe.tcc.model.Cliente;
import br.com.unifebe.tcc.model.Pessoa;
import br.com.unifebe.tcc.model.Produto;
import br.com.unifebe.tcc.model.Usuario;
import br.com.unifebe.tcc.model.Venda;
import br.com.unifebe.tcc.model.VendaCancelamento;
import br.com.unifebe.tcc.model.VendaItem;

/**
 * Hibernate Utility class with a convenient method to get Session Factory object.
 *
 * @author Carlos
 */
public class HibernateUtils {
	private static final SessionFactory sessionFactory;

	static {
		try {
			// Create the SessionFactory from standard (hibernate.cfg.xml) 
			// config file.

			sessionFactory = new AnnotationConfiguration().
			addAnnotatedClass( Pessoa.class ).
			addAnnotatedClass( Cliente.class ).
			addAnnotatedClass( Produto.class ).
			addAnnotatedClass( Usuario.class ).
			addAnnotatedClass( Venda.class ).
			addAnnotatedClass( VendaItem.class ).
			addAnnotatedClass( VendaCancelamento.class ).
			configure().buildSessionFactory();
			
		} catch (Throwable ex) {
			// Log the exception. 
			System.err.println("Initial SessionFactory creation failed." + ex);
			throw new ExceptionInInitializerError(ex);
		}
	}

	public static SessionFactory getSessionFactory() {
		return sessionFactory;
	}
}

